
// ---------------------------------------------------
// | File Name   : menu.c
// | Author   	 : chagn7z
// | Create Date : 2014-6-17
// ---------------------------------------------------

#include "stm32f10x.h"
#include "menu.h"
#include "include.h"
#include "user_cfg.h"

work_struct work_info = {1, 0, {0, 16, 8}, {0, 16, 8}, {20, 8}, {40, 23}, 0, 1, {1, 1}, {1, 1}};
window wnd_info = {WND_WELCOME, 0, onMenuFun, onUpFun, onDownFun, onEnterFun};
u8 key_status = 0;			// |0x00|menu|up|down|enter|

static btn_fun wnd_fun[MENU_NUM] = 
{
	/* welcome window */
	{
		menu11OnMenu, menu11OnUp, menu11OnDown, menu11OnEnter
	},

	/* select option */
	{
		menu21OnMenu, menu21OnUp, menu21OnDown, menu21OnEnter
	},	

	/* time setting */
	{
		menu31OnMenu, menu31OnUp, menu31OnDown, menu31OnEnter
	},

	/* relay working mode setting */
	{
		menu32OnMenu, menu32OnUp, menu32OnDown, menu32OnEnter
	},

	/* relay A setting */
	{
		menu41OnMenu, menu41OnUp, menu41OnDown, menu41OnEnter
	},

	/* relay B1&B2 setting */
	{
		menu42OnMenu, menu42OnUp, menu42OnDown, menu42OnEnter
	}
};

static u8 menu_str[16][16] =
{
	" Select Option  ",
	" 1:Time 2:Relay ",
	"    Set Time    ",
	"    00:00:00    ",
	"    Set Relay   ",
	" 1:A 2:B1 3:B2  ",
	"   Set Relay A  ",
	"  00:00--00:00  ",
	"   Set Relay B  ",
	"   Once A Day   ",
	"  Twice A Day   ",
	"  Once Two Days ",
	" Once Three Days",
	" Once Four Days ",
	"   Stop Work    ",
	"    Welcome     "
};

void welcomeWnd(void)
{
	u8 i;

	lcd_clear();

	lcd_setcursor(0, 0, 0);

	for (i=0; i<16; i++)
		lcd_putchar(1, i, menu_str[STR_WELCOME][i]);

	for (i=0; i<16; i++)
		lcd_putchar(2, i, menu_str[STR_TIME+1][i]);	

}

void timeupdata(void)
{
	lcd_setcursor(0, 0, 0);
	lcd_putchar(2, 4, work_info.time[2]/10);
	lcd_putchar(2, 5, work_info.time[2]%10);

	lcd_putchar(2, 7, work_info.time[1]/10);
	lcd_putchar(2, 8, work_info.time[1]%10);

	lcd_putchar(2, 10, work_info.time[0]/10);
	lcd_putchar(2, 11, work_info.time[0]%10);
}

//--------------welcome window-----------------------------------
void menu11OnMenu(void)
{
	u8 i,j;

	lcd_clear();

	lcd_setcursor(0, 0, 0);

	for (j=0; j<2; j++)
		for (i=0; i<16; i++)
			lcd_putchar(j+1, i, menu_str[STR_SELECT+j][i]);

	lcd_setcursor(2, 1, 1);

	wnd_info.pos      = 1;
	wnd_info.cur_menu = WND_SELECT;
}

void menu11OnUp(void)
{

}

void menu11OnDown(void)
{

}

void menu11OnEnter(void)
{

}

//--------------select option------------------------------------
void menu21OnMenu(void)
{
	wnd_info.pos      = 0;
	wnd_info.cur_menu = WND_WELCOME;
	lcd_setcursor(0, 0, 0);
	welcomeWnd();
}

void menu21OnUp(void)
{
	if (wnd_info.pos == 1)
	{
		wnd_info.pos = 2;
		lcd_setcursor(2, 8, 1);
	}
	else
	{
		wnd_info.pos = 1;
		lcd_setcursor(2, 1, 1);
	}
}

void menu21OnDown(void)
{
	if (wnd_info.pos == 1)
	{
		wnd_info.pos = 2;
		lcd_setcursor(2, 8, 1);
	}
	else
	{
		wnd_info.pos = 1;
		lcd_setcursor(2, 1, 1);
	}
}

void menu21OnEnter(void)
{
	u8 i,j;

	lcd_clear();

	if (wnd_info.pos == 1)
	{
		wnd_info.cur_menu = WND_TIME;
		work_info.modify   = 0;

		lcd_setcursor(0, 0, 0);

		for (j=0; j<2; j++)
			for (i=0; i<16; i++)
				lcd_putchar(j+1, i, menu_str[STR_TIME+j][i]);	

		// read time info write to work_info
		rtc_read();

		work_info.timeset[0] = work_info.time[0];
		work_info.timeset[1] = work_info.time[1];
		work_info.timeset[2] = work_info.time[2];

		lcd_putchar(2, 4, work_info.time[2]/10);
		lcd_putchar(2, 5, work_info.time[2]%10);

		lcd_putchar(2, 7, work_info.time[1]/10);
		lcd_putchar(2, 8, work_info.time[1]%10);

		wnd_info.pos = 1;
		lcd_setcursor(2, 5, 0);
		lcd_setline(1);
	}
	else
	{
		wnd_info.cur_menu = WND_MODE;

		for (j=0; j<2; j++)
			for (i=0; i<16; i++)
				lcd_putchar(j+1, i, menu_str[STR_MODE+j][i]);		

		wnd_info.pos = 1;
		lcd_setcursor(2, 1, 1);	
	}

}

//--------------time setting-------------------------------------
void menu31OnMenu(void)
{
	wnd_info.pos      = 0;
	wnd_info.cur_menu = WND_WELCOME;
	lcd_setcursor(0, 0, 0);
	rtc_write();
	welcomeWnd();
}

void menu31OnUp(void)
{
	if (work_info.modify)
	{
		if (wnd_info.pos == 1)
		{
			work_info.timeset[2]++;

			if (work_info.timeset[2] >= 24)
				work_info.timeset[2] = 0;

			lcd_putchar(2, 4, work_info.timeset[2]/10);
			lcd_putchar(2, 5, work_info.timeset[2]%10);			
		}
		else if (wnd_info.pos == 2)
		{
			work_info.timeset[1]++;

			if (work_info.timeset[1] >= 60)
				work_info.timeset[1] = 0;

			lcd_putchar(2, 7, work_info.timeset[1]/10);
			lcd_putchar(2, 8, work_info.timeset[1]%10);			
		}
		else if (wnd_info.pos == 3)
		{
			work_info.timeset[0]++;

			if (work_info.timeset[0] >= 60)
				work_info.timeset[0] = 0;

			lcd_putchar(2, 10, work_info.timeset[0]/10);
			lcd_putchar(2, 11, work_info.timeset[0]%10);			
		}

		if (wnd_info.pos == 1)
			lcd_setcursor(2, 5, 1);
		else if (wnd_info.pos == 2)
			lcd_setcursor(2, 8, 1);
		else if (wnd_info.pos == 3)
			lcd_setcursor(2, 11, 1);		
	}
	else
	{
		wnd_info.pos++;

		if (wnd_info.pos > 3)
			wnd_info.pos = 1;

		if (wnd_info.pos == 1)
			lcd_setcursor(2, 5, 0);
		else if (wnd_info.pos == 2)
			lcd_setcursor(2, 8, 0);
		else if (wnd_info.pos == 3)
			lcd_setcursor(2, 11, 0);		

		lcd_setline(1);
	}

	// if (wnd_info.pos == 1)
	// 	lcd_setcursor(2, 5, 1);
	// else if (wnd_info.pos == 2)
	// 	lcd_setcursor(2, 8, 1);
	// else if (wnd_info.pos == 3)
	// 	lcd_setcursor(2, 11, 1);
}

void menu31OnDown(void)
{
	if (work_info.modify)
	{
		if (wnd_info.pos == 1)
		{
			work_info.timeset[2]--;

			if (work_info.timeset[2] == 255)
				work_info.timeset[2] = 23;

			lcd_putchar(2, 4, work_info.timeset[2]/10);
			lcd_putchar(2, 5, work_info.timeset[2]%10);
		}
		else if (wnd_info.pos == 2)
		{
			work_info.timeset[1]--;

			if (work_info.timeset[1] == 255)
				work_info.timeset[1] = 59;

			lcd_putchar(2, 7, work_info.timeset[1]/10);
			lcd_putchar(2, 8, work_info.timeset[1]%10);			
		}
		else if (wnd_info.pos == 3)
		{
			work_info.timeset[0]--;

			if (work_info.timeset[0] == 255)
				work_info.timeset[0] = 59;

			lcd_putchar(2, 10, work_info.timeset[0]/10);
			lcd_putchar(2, 11, work_info.timeset[0]%10);		
		}

		if (wnd_info.pos == 1)
			lcd_setcursor(2, 5, 1);
		else if (wnd_info.pos == 2)
			lcd_setcursor(2, 8, 1);
		else if (wnd_info.pos == 3)
			lcd_setcursor(2, 11, 1);			
	}
	else
	{
		wnd_info.pos--;

		if (wnd_info.pos == 0)
			wnd_info.pos = 3;

		if (wnd_info.pos == 1)
			lcd_setcursor(2, 5, 0);
		else if (wnd_info.pos == 2)
			lcd_setcursor(2, 8, 0);
		else if (wnd_info.pos == 3)
			lcd_setcursor(2, 11, 0);			

		lcd_setline(1);
	}

	// if (wnd_info.pos == 1)
	// 	lcd_setcursor(2, 5, 1);
	// else if (wnd_info.pos == 2)
	// 	lcd_setcursor(2, 8, 1);
	// else if (wnd_info.pos == 3)
	// 	lcd_setcursor(2, 11, 1);	
}

void menu31OnEnter(void)
{
	if (work_info.modify)
	{
		work_info.modify = 0;
		/* close flash block */
		lcd_setline(1);
	}
	else
	{
		work_info.modify = 1;

		lcd_setline(0);

		if (wnd_info.pos == 1)
			lcd_setcursor(2, 5, 1);
		else if (wnd_info.pos == 2)
			lcd_setcursor(2, 8, 1);
		else if (wnd_info.pos == 3)
			lcd_setcursor(2, 11, 1);
	}
}

//--------------relay setting------------------------------------
void menu32OnMenu(void)
{
	wnd_info.pos      = 0;
	wnd_info.cur_menu = WND_WELCOME;
	lcd_setcursor(0, 0, 0);
	welcomeWnd();
}

void menu32OnUp(void)
{
	wnd_info.pos++;
	if (wnd_info.pos > 3)
		wnd_info.pos = 1;

	if (wnd_info.pos == 1)
		lcd_setcursor(2, 1, 1);
	else if (wnd_info.pos == 2)
		lcd_setcursor(2, 5, 1);
	else if (wnd_info.pos == 3)
		lcd_setcursor(2, 10, 1);
}

void menu32OnDown(void)
{
	wnd_info.pos--;
	if (wnd_info.pos == 0)
		wnd_info.pos = 3;

	if (wnd_info.pos == 1)
		lcd_setcursor(2, 1, 1);
	else if (wnd_info.pos == 2)
		lcd_setcursor(2, 5, 1);
	else if (wnd_info.pos == 3)
		lcd_setcursor(2, 10, 1);
}

void menu32OnEnter(void)
{
	u8 i,j;
	u8 idx;

	if (wnd_info.pos == 1)
	{
		wnd_info.cur_menu = WND_RELAYA;
		work_info.modify   = 0;

		for (j=0; j<2; j++)
			for (i=0; i<16; i++)
				lcd_putchar(j+1, i, menu_str[STR_RELAYA+j][i]);

		lcd_putchar(2, 2, work_info.relaya_on[1]/10);
		lcd_putchar(2, 3, work_info.relaya_on[1]%10);

		lcd_putchar(2, 5, work_info.relaya_on[0]/10);
		lcd_putchar(2, 6, work_info.relaya_on[0]%10);

		lcd_putchar(2, 9, work_info.relaya_off[1]/10);
		lcd_putchar(2, 10, work_info.relaya_off[1]%10);

		lcd_putchar(2, 12, work_info.relaya_off[0]/10);
		lcd_putchar(2, 13, work_info.relaya_off[0]%10);

		lcd_setcursor(2, 3, 0);
		lcd_setline(1);
	}
	else if (wnd_info.pos == 2)
	{
		wnd_info.cur_menu   = WND_RELAYB;
		work_info.relay_num = 1;

		for (i=0; i<16; i++)
			lcd_putchar(1, i, menu_str[STR_RELAYB][i]);	
		lcd_putchar(1, 14, 1);

		idx = STR_RELAYB+work_info.relayb_mode[work_info.relay_num-1];
		for (i=0; i<16; i++)
			lcd_putchar(2, i, menu_str[idx][i]);

		lcd_setcursor(0, 0, 0);
	}
	else if (wnd_info.pos == 3)
	{
		wnd_info.cur_menu   = WND_RELAYB;
		work_info.relay_num = 2;

		for (i=0; i<16; i++)
			lcd_putchar(1, i, menu_str[STR_RELAYB][i]);	
		lcd_putchar(1, 14, 2);

		idx = STR_RELAYB+work_info.relayb_mode[work_info.relay_num-1];
		for (i=0; i<16; i++)
			lcd_putchar(2, i, menu_str[idx][i]);

		lcd_setcursor(0, 0, 0);
	}
}
//--------------relay A setting------------------------------------

void menu41OnMenu(void)
{
	wnd_info.pos      = 0;
	wnd_info.cur_menu = WND_WELCOME;
	lcd_setcursor(0, 0, 0);
	welcomeWnd();
}

void menu41OnUp(void)
{
	if (work_info.modify)
	{
		if (wnd_info.pos == 1)
		{
			work_info.relaya_on[1]++;

			if (work_info.relaya_on[1] >= 24)
				work_info.relaya_on[1] = 0;

			lcd_putchar(2, 2, work_info.relaya_on[1]/10);
			lcd_putchar(2, 3, work_info.relaya_on[1]%10);			
		}
		else if (wnd_info.pos == 2)
		{
			work_info.relaya_on[0]++;

			if (work_info.relaya_on[0] >= 60)
				work_info.relaya_on[0] = 0;

			lcd_putchar(2, 5, work_info.relaya_on[0]/10);
			lcd_putchar(2, 6, work_info.relaya_on[0]%10);			
		}
		else if (wnd_info.pos == 3)
		{
			work_info.relaya_off[1]++;

			if (work_info.relaya_off[1] >= 24)
				work_info.relaya_off[1] = 0;

			lcd_putchar(2, 9, work_info.relaya_off[1]/10);
			lcd_putchar(2, 10, work_info.relaya_off[1]%10);			
		}
		else if (wnd_info.pos == 4)
		{
			work_info.relaya_off[0]++;

			if (work_info.relaya_off[0] >= 60)
				work_info.relaya_off[0] = 0;

			lcd_putchar(2, 12, work_info.relaya_off[0]/10);
			lcd_putchar(2, 13, work_info.relaya_off[0]%10);			
		}

		if (wnd_info.pos == 1)
			lcd_setcursor(2, 3, 1);
		else if (wnd_info.pos == 2)
			lcd_setcursor(2, 6, 1);
		else if (wnd_info.pos == 3)
			lcd_setcursor(2, 10, 1);
		else if (wnd_info.pos == 4)
			lcd_setcursor(2, 13, 1);

	}
	else
	{
		wnd_info.pos++;

		if (wnd_info.pos > 4)
			wnd_info.pos = 1;

		if (wnd_info.pos == 1)
			lcd_setcursor(2, 3, 0);
		else if (wnd_info.pos == 2)
			lcd_setcursor(2, 6, 0);
		else if (wnd_info.pos == 3)
			lcd_setcursor(2, 10, 0);
		else if (wnd_info.pos == 4)
			lcd_setcursor(2, 13, 0);

		lcd_setline(1);
	}

	// if (wnd_info.pos == 1)
	// 	lcd_setcursor(2, 3, 1);
	// else if (wnd_info.pos == 2)
	// 	lcd_setcursor(2, 6, 1);
	// else if (wnd_info.pos == 3)
	// 	lcd_setcursor(2, 10, 1);
	// else if (wnd_info.pos == 4)
	// 	lcd_setcursor(2, 13, 1);
}

void menu41OnDown(void)
{
	if (work_info.modify)
	{
		if (wnd_info.pos == 1)
		{
			work_info.relaya_on[1]--;

			if (work_info.relaya_on[1] == 255)
				work_info.relaya_on[1] = 23;

			lcd_putchar(2, 2, work_info.relaya_on[1]/10);
			lcd_putchar(2, 3, work_info.relaya_on[1]%10);			
		}
		else if (wnd_info.pos == 2)
		{
			work_info.relaya_on[0]--;

			if (work_info.relaya_on[0] == 255)
				work_info.relaya_on[0] = 59;

			lcd_putchar(2, 5, work_info.relaya_on[0]/10);
			lcd_putchar(2, 6, work_info.relaya_on[0]%10);			
		}
		else if (wnd_info.pos == 3)
		{
			work_info.relaya_off[1]--;

			if (work_info.relaya_off[1] == 255)
				work_info.relaya_off[1] = 23;

			lcd_putchar(2, 9, work_info.relaya_off[1]/10);
			lcd_putchar(2, 10, work_info.relaya_off[1]%10);			
		}
		else if (wnd_info.pos == 4)
		{
			work_info.relaya_off[0]--;

			if (work_info.relaya_off[0] == 255)
				work_info.relaya_off[0] = 59;

			lcd_putchar(2, 12, work_info.relaya_off[0]/10);
			lcd_putchar(2, 13, work_info.relaya_off[0]%10);			
		}	

		if (wnd_info.pos == 1)
			lcd_setcursor(2, 3, 1);
		else if (wnd_info.pos == 2)
			lcd_setcursor(2, 6, 1);
		else if (wnd_info.pos == 3)
			lcd_setcursor(2, 10, 1);
		else if (wnd_info.pos == 4)	
			lcd_setcursor(2, 13, 1);

	}
	else
	{
		wnd_info.pos--;

		if (wnd_info.pos == 0)
			wnd_info.pos = 4;

		if (wnd_info.pos == 1)
			lcd_setcursor(2, 3, 0);
		else if (wnd_info.pos == 2)
			lcd_setcursor(2, 6, 0);
		else if (wnd_info.pos == 3)
			lcd_setcursor(2, 10, 0);
		else if (wnd_info.pos == 4)	
			lcd_setcursor(2, 13, 0);	
			
		lcd_setline(1);
	}

	// if (wnd_info.pos == 1)
	// 	lcd_setcursor(2, 3, 1);
	// else if (wnd_info.pos == 2)
	// 	lcd_setcursor(2, 6, 1);
	// else if (wnd_info.pos == 3)
	// 	lcd_setcursor(2, 10, 1);
	// else if (wnd_info.pos == 4)	
	// 	lcd_setcursor(2, 13, 1);
}

void menu41OnEnter(void)
{
	if (work_info.modify)
	{
		work_info.modify = 0;
		/* close flash block */

		lcd_setline(1);
	}
	else
	{
		work_info.modify = 1;

		lcd_setline(0);

		if (wnd_info.pos == 1)
			lcd_setcursor(2, 3, 1);
		else if (wnd_info.pos == 2)
			lcd_setcursor(2, 6, 1);
		else if (wnd_info.pos == 3)
			lcd_setcursor(2, 10, 1);
		else if (wnd_info.pos == 4)
			lcd_setcursor(2, 13, 1);
	}
}


//--------------relay B1/2 setting-------------------------------
void menu42OnMenu(void)
{
	wnd_info.pos      = 0;
	wnd_info.cur_menu = WND_WELCOME;
	lcd_setcursor(0, 0, 0);
	welcomeWnd();
}

void menu42OnUp(void)
{
	u8 idx;
	u8 i;

	work_info.relayb_mode[work_info.relay_num-1]++;

	if (work_info.relayb_mode[work_info.relay_num-1] >= 7)
		work_info.relayb_mode[work_info.relay_num-1] = 1;

	idx = STR_RELAYB + work_info.relayb_mode[work_info.relay_num-1];
	for (i=0; i<16; i++)
		lcd_putchar(2, i, menu_str[idx][i]);
}

void menu42OnDown(void)
{
	u8 idx;
	u8 i;

	work_info.relayb_mode[work_info.relay_num-1]--;

	if (work_info.relayb_mode[work_info.relay_num-1] == 0)
		work_info.relayb_mode[work_info.relay_num-1] = 6;

	idx = STR_RELAYB + work_info.relayb_mode[work_info.relay_num-1];
	for (i=0; i<16; i++)
		lcd_putchar(2, i, menu_str[idx][i]);
}

void menu42OnEnter(void)
{
	wnd_info.pos      = 0;
	wnd_info.cur_menu = WND_WELCOME;
	lcd_setcursor(0, 0, 0);
	welcomeWnd();
}

//--------------button function----------------------------------
void onMenuFun(void)
{
	// lcd_light_sec = 0;
	// LCD_LIGHT = 1;

	wnd_fun[wnd_info.cur_menu].onMenu();
}

void onUpFun(void)
{
	// lcd_light_sec = 0;
	// LCD_LIGHT = 1;

	wnd_fun[wnd_info.cur_menu].onUp();
}

void onDownFun(void)
{
	// lcd_light_sec = 0;
	// LCD_LIGHT = 1;

	wnd_fun[wnd_info.cur_menu].onDown();
}

void onEnterFun(void)
{
	// lcd_light_sec = 0;
	// LCD_LIGHT = 1;

	wnd_fun[wnd_info.cur_menu].onEnter();
}

u8 key_scan(void)
{
	// if (!KEY_MENU || !KEY_UP || !KEY_DOWN || !KEY_ENTER)
	// {
	// 	delay_us(15000);

	// 	if (!KEY_MENU)
	// 		return MENU_DOWN;
	// 	else if (!KEY_UP)
	// 		return UP_DOWN;
	// 	else if (!KEY_DOWN)
	// 		return DOWN_DOWN;		
	// 	else if (!KEY_ENTER)
	// 		return ENTER_DOWN;		
	// }
	u8 key_temp;

	key_temp = key_status;

	if (key_temp)
	{
		key_status = 0;

		if (key_temp & 0x08)
			return MENU_DOWN;
		else if (key_temp & 0x04)
			return UP_DOWN;
		else if (key_temp & 0x02)
			return DOWN_DOWN;
		else if (key_temp & 0x01)
			return ENTER_DOWN;
	}

	return 0;
}

void key_deal(void)
{
	u8 key_id;
	void (*key)();

	key    = 0;
	key_id = key_scan();

	switch (key_id)
	{
		case MENU_DOWN:
			key = wnd_info.onMenuFun;
		break;
		case UP_DOWN:
			key = wnd_info.onUpFun;
		break;
		case DOWN_DOWN:
			key = wnd_info.onDownFun;
		break;
		case ENTER_DOWN:
			key = wnd_info.onEnterFun;
		break;
	}

	if (key != 0)
		key();
}

void EXTI9_5_IRQHandler(void)
{
	delay_us(10000);

	if(EXTI_GetITStatus(EXTI_Line6)!=RESET)
	{
		if (!KEY_ENTER)
			key_status |= 0x01;
		
		EXTI_ClearITPendingBit(EXTI_Line6); 
	}

	if(EXTI_GetITStatus(EXTI_Line7)!=RESET)
	{
		if (!KEY_DOWN)
			key_status |= 0x02;
		
		EXTI_ClearITPendingBit(EXTI_Line7); 
	}
}

void EXTI15_10_IRQHandler(void)
{
	delay_us(10000);

	if(EXTI_GetITStatus(EXTI_Line14)!=RESET)
	{
		if (!KEY_UP)
			key_status |= 0x04;

		EXTI_ClearITPendingBit(EXTI_Line14); 
	}

	if(EXTI_GetITStatus(EXTI_Line15)!=RESET)
	{
		if (!KEY_MENU)
			key_status |= 0x08;

		EXTI_ClearITPendingBit(EXTI_Line15); 
	}
}
